//
// Created by Ivor_ on 2023/2/6.
//

#include "complexVariable.h"

void juliaSet(char* fileName, double plotLimit, int mm, _Complex double iterConst) {
    int lg = (int)floor(log10(plotLimit * 2.4));
    int pixelSize = (int)ceil(plotLimit * 2.4 * pow(10., 2. - lg));
    PIXEL whitePixel = {0xff, 0xff, 0xff};
    PIXEL blackPixel = {0x0, 0x0, 0x0};
    PIXEL bluePixel = {0x0, 0x0, 0xff};
    STDbmp* picture = bmpBO().new(fileName, pixelSize, pixelSize, whitePixel);
    for (int i = -(int)ceil(plotLimit * pow(10., 2. - lg)); i <= (int)ceil(plotLimit * pow(10., 2. - lg)); ++i) {
        for (int j = -(int)ceil(plotLimit * pow(10., 2. - lg)); j <= (int)ceil(plotLimit * pow(10., 2. - lg)); ++j) {
            _Complex double zz = (i + .5)/pow(10., 2.-lg) + (j + .5)/pow(10., 2.-lg)*I;
            unsigned int counter = 0;
            for (int k = 0; k < 128; ++k) {
                zz = cpow(zz, 1.*mm) + iterConst;
                if (cabs(zz) <= plotLimit) {
                    counter++;
                }
            }
            bluePixel.red = 0x7f - counter;
            bluePixel.green = 0x7f - counter*counter;
            bmpBO().drawPoint(picture, i+(int)ceil(plotLimit * 1.2 * pow(10., 2. - lg)), j + (int)ceil(plotLimit * 1.2 * pow(10., 2. - lg)), 1, 1, bluePixel);
        }
    }
    bmpBO().drawLine(picture, pixelSize/2, 3, (int)ceil(plotLimit * .2 * pow(10., 2. - lg)), blackPixel);
    bmpBO().drawRow(picture, pixelSize/2, 3, (int)ceil(plotLimit * .2 * pow(10., 2. - lg)), blackPixel);
}

void mandelbrotSet(char* fileName, double plotLimit, int mm, _Complex double iterConst) {
    int lg = (int)floor(log10(plotLimit * 2.4));
    int pixelSize = (int)ceil(plotLimit * 2.4 * pow(10., 2. - lg));
    PIXEL whitePixel = {0xff, 0xff, 0xff};
    PIXEL blackPixel = {0x0, 0x0, 0x0};
    PIXEL bluePixel = {0x0, 0x0, 0xff};
    STDbmp* picture = bmpBO().new(fileName, pixelSize, pixelSize, whitePixel);
    for (int i = -(int)ceil(plotLimit * pow(10., 2. - lg)); i <= (int)ceil(plotLimit * pow(10., 2. - lg)); ++i) {
        for (int j = -(int)ceil(plotLimit * pow(10., 2. - lg)); j <= (int)ceil(plotLimit * pow(10., 2. - lg)); ++j) {
            _Complex double zz = iterConst;
            unsigned int counter = 0;
            for (int k = 0; k < 128; ++k) {
                zz = cpow(zz, 1.*mm) + (i + .5)/pow(10., 2.-lg) + (j + .5)/pow(10., 2.-lg)*I;
                if (cabs(zz) <= plotLimit) {
                    counter++;
                }
            }
            bluePixel.red = 0x7f - counter;
            bluePixel.green = 0x7f - counter*counter;
            bmpBO().drawPoint(picture, i+(int)ceil(plotLimit * 1.2 * pow(10., 2. - lg)), j + (int)ceil(plotLimit * 1.2 * pow(10., 2. - lg)), 1, 1, bluePixel);
        }
    }
    bmpBO().drawLine(picture, pixelSize/2, 3, (int)ceil(plotLimit * .2 * pow(10., 2. - lg)), blackPixel);
    bmpBO().drawRow(picture, pixelSize/2, 3, (int)ceil(plotLimit * .2 * pow(10., 2. - lg)), blackPixel);
}
